package com.ext.aic.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ext.aic.R;
import com.ext.aic.beans.Attachment;
import com.ext.aic.beans.FlowItem;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.databinding.CopyWritingSelectItemBinding;
import com.ext.aic.handler.FlowHandler;
import com.ext.aic.holder.ReferenceCopyViewHolder;
import com.eyinfo.android_pure_utils.events.Action1;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;
import com.google.android.flexbox.FlexboxLayout;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import lombok.Setter;

public class ReferenceCopyListAdapter extends RecyclerView.Adapter<ReferenceCopyViewHolder> {

    private List<ProductItem> items = new ArrayList<>();
    private Context context;
    @Setter
    private Action1<ProductItem> itemAction;

    public ReferenceCopyListAdapter(Context context) {
        this.context = context;
    }

    public void addItems(List<ProductItem> items) {
        this.items.addAll(items);
    }

    public void clear() {
        items.clear();
    }

    @Override
    public ReferenceCopyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.copy_writing_select_item, parent, false);
        return new ReferenceCopyViewHolder(CopyWritingSelectItemBinding.bind(view));
    }

    @Override
    public void onBindViewHolder(ReferenceCopyViewHolder holder, int position) {
        ProductItem productItem = items.get(position);
        bindImage(productItem, holder.itemRiv);
        holder.titleTv.setText(productItem.getTitle());
        holder.descTv.setText(productItem.getBody());
        String recommendReason = productItem.getRecommendReason();
        if (!TextUtils.isEmpty(recommendReason)) {
            String reasons = String.format(context.getString(R.string.reasons_format), recommendReason);
            holder.reasonsTv.setText(reasons);
        }
        bindLabels(productItem, holder.labelFbl);
        if (productItem.isCheck()) {
            holder.checkedTv.setEnabled(true);
            holder.itemRl.setBackgroundResource(R.drawable.copy_writing_item_checked_bg);
        } else {
            holder.checkedTv.setEnabled(false);
            holder.itemRl.setBackgroundResource(R.drawable.item_normal_sixteen_bg);
        }
        holder.itemRl.setTag(position);
        holder.itemRl.setOnClickListener(this::onItemClick);
    }

    public void onItemClick(View view) {
        int position = ConvertUtils.toInt(view.getTag());
        if (itemAction != null) {
            itemAction.call(items.get(position));
        }
    }

    private void bindLabels(ProductItem productItem, FlexboxLayout labelFbl) {
        List<String> labels = productItem.getLabels();
        if (ObjectJudge.isNullOrEmpty(labels)) {
            return;
        }
        List<FlowItem> tabItems = new ArrayList<>();
        for (String label : labels) {
            tabItems.add(new FlowItem(label));
        }
        FlowHandler flowHandler = new FlowHandler();
        flowHandler.bindTags(labelFbl, tabItems);
    }

    private void bindImage(ProductItem productItem, RoundedImageView itemRiv) {
        List<Attachment> attachments = productItem.getAttachments();
        if (!ObjectJudge.isNullOrEmpty(attachments)) {
            Attachment attachment = attachments.get(0);
            Glide.with(context)
                    .load(attachment.getFileUrl())
                    .into(itemRiv);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
